<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\menu\util\row_events;

/**
 * Modal事件
 * @author cleverstone
 * @since ym2.0
 */
class Modal extends RowEventType
{
    /**
     * @var array 选项
     */
    private $_options = [
        'type' => 'modal',//支持modal/ajax
        'title' => '',
        'icon' => '',
        'route' => '',
        'ignoreRBAC' => false,//路由是否忽略RBAC校验
        'width' => '435px',
        'height' => '225px',
    ];

    /**
     * 标题
     * @param string $title
     * @return $this
     */
    public function title($title)
    {
        $this->_options['title'] = (string)$title;
        return $this;
    }

    /**
     * @param string $icon 图标
     * 支持的图标有:
     * - bootstrap v4图标
     * - fontawesome v5
     * - layui 2.8图标
     *
     * @return $this
     */
    public function icon($icon)
    {
        $this->_options['icon'] = $icon;
        return $this;
    }

    /**
     * @param string|array $route 路由或外链
     * ```php
     * 可以直接指定路由
     * $this->route('user/edit');
     *
     * 也可以指定外部链接
     * $this->route('https://ym2.cleverstone.top');
     *
     * ```
     * @return $this
     */
    public function route($route, $ignoreRBAC = false)
    {
        $this->_options['route'] = $route;
        $this->_options['ignoreRBAC'] = (boolean)$ignoreRBAC;
        return $this;
    }

    /**
     * @param string|int $width 模态框的宽, 当type为modal时有效;
     * @return $this
     */
    public function width($width = '100%')
    {
        if (is_numeric($width)) {
            $width .= 'px';
        }

        $this->_options['width'] = $width;
        return $this;
    }

    /**
     * @param string|int $height 模态框的高, 当type为modal时有效;
     * @return $this
     */
    public function height($height = '100%')
    {
        if (is_numeric($height)) {
            $height .= 'px';
        }

        $this->_options['height'] = $height;
        return $this;
    }

    /**
     * @return array
     */
    public function _toArray()
    {
        if (
            $this->_options['route']
            && !$this->_options['ignoreRBAC']
            && !$this->checkAccess($this->_options['route'])
        ) {
            return [];
        }

        return $this->_options;
    }
}